﻿<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>讯言</title>
    <link type="text/css" rel="stylesheet" href="css/login.css" />
    <link type="text/css" rel="stylesheet" href="css/public.css" />
</head>
</head>
<body class="login_bg">
    <div class="nav">讯言</div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>登录</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="userpwd">
            </div>
            <div class="row">
                <button id="submit" onclick="login()">登录</button>
                <button id="register" onclick="register()">注册</button>
            </div>
        </div>
    </div>

    <script src="http://mtc.baidu.com/static/common/lib/jquery/jquery.min_7e57d31.js"></script>
    <script>
         function login() {
            $.ajax({
                type:"post",
                url:"/user/login",
                data:{
                    "userName":$("#username").val(),
                    "password":$("#userpwd").val()
                },
                success:function(result){
                    if(result.code==200 && result.data!=null){
                        console.log(JSON.stringify(result.data));
                        localStorage.setItem("user_token",JSON.stringify(result.data));//存储数据
                        location.assign("client.html");
                    }else{
                        alert("用户名或密码错误");
                        $("#username").val("");
                        $("#userpwd").val("");
                        return;
                    }
                }
            });
        }

        function register(){
            $.ajax({
                type:"post",
                url:"/user/register",
                data:{
                    "userName":$("#username").val(),
                    "password":$("#userpwd").val()
                },
                success:function(result){
                    if(result.code==200 && result.data==true){
                        alert("注册成功，请重新登录！");
                        $("#username").val("");
                        $("#userpwd").val("");
                    }else if(result.code==-1 && result.data==null){
                       alert(result.errmsg);
                       $("#username").val("");
                       $("#userpwd").val("");
                    }
                }
            });
        }
    </script>
</body>
</html>
